package io.renren.modules.app.controller;


import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import io.renren.common.utils.R;
import io.renren.config.rabbitmq.FirstSender;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * APP测试接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:47
 */
@RestController
@RequestMapping("/app")
@Api("APP测试接口")
public class AppTestController {
    @Autowired
    private UserService userService;

    /*@Autowired
    private MongoTemplate mongoTemplate;*/

    @Autowired
    private FirstSender firstSender;

    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser UserEntity user){
        return R.ok().put("user", user);
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken(){

        /*System.out.println("start" );
        userService.addUser();
        System.out.println("end");
        userService.addUser2();*/

        /*int i = 0;
        while (i < 100000){
            UserEntity user = new UserEntity();
            int t = (int)(1+Math.random()*(100000-1+1));
            System.out.println("随机数" + t);
            user.setUsername("zuoqing"+t);
            user.setUserId(100000001L);
            mongoTemplate.save(user,"userCollection");
            i++;
        }*/

        /*Long l = System.currentTimeMillis();
        UserEntity user3 = mongoTemplate.findOne(new Query(Criteria.where("username").is("zuoqing4873")),UserEntity.class,"userCollection");
        System.out.println(user3.getUsername());
        System.out.println("花费时间:" + (System.currentTimeMillis() - l));*/

        /*String jsonString = JSONObject.toJSONString(user);
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        mongoTemplate.save(jsonObject,"userJsonObject");

        JSONObject jsonObject1 = mongoTemplate.findOne(new Query(Criteria.where("userId").is(10000000L)),JSONObject.class,"userJsonObject");

        System.out.println(jsonObject1.toString());

        UserEntity user4 = JSON.parseObject(jsonObject1.toJSONString(),UserEntity.class);

        System.out.println(user4.getUsername());


        HashMap<String,Object> params = new HashMap<>();
        params.put("userName","力问题闹");
        params.put("userId",23424234);
        mongoTemplate.save(params,"userMap");


        HashMap<String,Object> h = mongoTemplate.findOne(new Query(Criteria.where("user").), HashMap.class,"userMap");

        System.out.println(h.toString());*/

///*        #   data:
//#        mongodb:
//#            host: 127.0.0.1
//#            port: 27017
//#            database: admin*/


        return R.ok().put("msg", "无需token也能访问。。。");
    }



    @GetMapping("/send")
    public String send(String message){
        String uuid = UUID.randomUUID().toString();

        UserEntity user = new UserEntity();
        user.setUserId(1L);
        user.setUsername("刘德华");

        firstSender.send(uuid,JSON.toJSONString(user));
        return uuid;
    }


}
